package com.yc.AdminUI;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;

import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.AddCarDao;
import com.yc.dao.UserControlDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

/**
 * 添加修改列车
 * @author UNO
 * 2021年6月24日下午7:03:14
 */
public class AddCar extends Composite {
	private Table table;
	private Text text_2;
	private Text text_4;
	private Text text_5;
	private Text text_6;
	private Text text;
	private Text text_3;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AddCar(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setLayout(new FormLayout());

		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(0, 238);
		fd_table.right = new FormAttachment(0, 718);
		fd_table.top = new FormAttachment(0, 10);
		fd_table.left = new FormAttachment(0, 23);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(74);
		tableColumn.setText("列车编号");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(79);
		tableColumn_1.setText("列车名称");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("起始站");

		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("终点站");

		TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
		tableColumn_4.setWidth(77);
		tableColumn_4.setText("出发时间");

		TableColumn tableColumn_5 = new TableColumn(table, SWT.NONE);
		tableColumn_5.setWidth(110);
		tableColumn_5.setText("预计到达时间");

		TableColumn tableColumn_6 = new TableColumn(table, SWT.NONE);
		tableColumn_6.setWidth(100);
		tableColumn_6.setText("状态");

		TableCursor tableCursor = new TableCursor(table, SWT.NONE);

		Menu menu = new Menu(tableCursor);
		tableCursor.setMenu(menu);

		MenuItem menuItem = new MenuItem(menu, SWT.NONE);
		menuItem.setText("运行");

		MenuItem menuItem_1 = new MenuItem(menu, SWT.NONE);
		menuItem_1.setText("停运");

		Label label_2 = new Label(composite, SWT.NONE);
		FormData fd_label_2 = new FormData();
		fd_label_2.top = new FormAttachment(table, 32);
		label_2.setLayoutData(fd_label_2);
		label_2.setText("列车名称 ：");

		text_2 = new Text(composite, SWT.BORDER);
		fd_label_2.right = new FormAttachment(text_2, -26);
		FormData fd_text_2 = new FormData();
		fd_text_2.top = new FormAttachment(table, 29);
		fd_text_2.left = new FormAttachment(0, 402);
		text_2.setLayoutData(fd_text_2);

		Label label_3 = new Label(composite, SWT.NONE);
		fd_text_2.right = new FormAttachment(label_3, -24);
		FormData fd_label_3 = new FormData();
		fd_label_3.top = new FormAttachment(table, 32);
		label_3.setLayoutData(fd_label_3);
		label_3.setText("起始站 ：");
		fd_label_3.right = new FormAttachment(100, -297);

		Label label_4 = new Label(composite, SWT.NONE);
		FormData fd_label_4 = new FormData();
		fd_label_4.left = new FormAttachment(0, 23);
		label_4.setLayoutData(fd_label_4);
		label_4.setText("终点站 ：");

		text_4 = new Text(composite, SWT.BORDER);
		fd_label_4.right = new FormAttachment(text_4, -4);
		FormData fd_text_4 = new FormData();
		fd_text_4.left = new FormAttachment(0, 85);
		fd_text_4.top = new FormAttachment(label_4, -3, SWT.TOP);
		text_4.setLayoutData(fd_text_4);

		Label label_5 = new Label(composite, SWT.NONE);
		fd_text_4.right = new FormAttachment(label_5, -38);
		FormData fd_label_5 = new FormData();
		fd_label_5.left = new FormAttachment(0, 257);
		fd_label_5.top = new FormAttachment(label_4, 0, SWT.TOP);
		label_5.setLayoutData(fd_label_5);
		label_5.setText("出发时间 ：");

		text_5 = new Text(composite, SWT.BORDER);
		fd_label_5.right = new FormAttachment(100, -544);
		FormData fd_text_5 = new FormData();
		fd_text_5.left = new FormAttachment(label_5, 6);
		fd_text_5.top = new FormAttachment(label_4, -3, SWT.TOP);
		text_5.setLayoutData(fd_text_5);

		Label label_6 = new Label(composite, SWT.NONE);
		fd_text_5.right = new FormAttachment(label_6, -26);
		FormData fd_label_6 = new FormData();
		fd_label_6.top = new FormAttachment(label_4, 0, SWT.TOP);
		fd_label_6.right = new FormAttachment(label_3, 0, SWT.RIGHT);
		fd_label_6.left = new FormAttachment(0, 481);
		label_6.setLayoutData(fd_label_6);
		label_6.setText("预计到达时间 ：");

		text_6 = new Text(composite, SWT.BORDER);
		FormData fd_text_6 = new FormData();
		fd_text_6.right = new FormAttachment(100, -164);
		fd_text_6.left = new FormAttachment(0, 590);
		fd_text_6.top = new FormAttachment(label_4, -3, SWT.TOP);
		text_6.setLayoutData(fd_text_6);

		Button button_1 = new Button(composite, SWT.NONE);
		FormData fd_button_1 = new FormData();
		fd_button_1.bottom = new FormAttachment(100, -53);
		fd_button_1.right = new FormAttachment(100, -188);
		button_1.setLayoutData(fd_button_1);
		button_1.setText("添加");

		Label label_7 = new Label(composite, SWT.NONE);
		FormData fd_label_7 = new FormData();
		fd_label_7.top = new FormAttachment(label_2, 0, SWT.TOP);
		fd_label_7.left = new FormAttachment(table, 0, SWT.LEFT);
		label_7.setLayoutData(fd_label_7);
		label_7.setText("列车类型：");

		Combo combo = new Combo(composite, SWT.NONE);
		fd_label_2.left = new FormAttachment(combo, 36);
		fd_text_2.bottom = new FormAttachment(combo, 0, SWT.BOTTOM);
		combo.setItems(new String[] {"直达特快旅客列车", "城际组列车", "快速旅客列车", "临时旅客列车", "城郊专运客车", "动车组列车", "旅客列车","高速铁路动车组"});
		FormData fd_combo = new FormData();
		fd_combo.top = new FormAttachment(table, 29);
		fd_combo.right = new FormAttachment(100, -613);
		fd_combo.left = new FormAttachment(label_7, 4);
		combo.setLayoutData(fd_combo);
		combo.setText("高速铁路动车组");

		Button button_2 = new Button(composite, SWT.NONE);
		FormData fd_button_2 = new FormData();
		fd_button_2.top = new FormAttachment(button_1, 0, SWT.TOP);
		button_2.setLayoutData(fd_button_2);
		button_2.setText("修改");

		Label label_8 = new Label(composite, SWT.NONE);
		fd_label_4.bottom = new FormAttachment(label_8, -54);
		FormData fd_label_8 = new FormData();
		fd_label_8.top = new FormAttachment(button_1, 5, SWT.TOP);
		label_8.setLayoutData(fd_label_8);
		label_8.setText("列车编号：");

		text = new Text(composite, SWT.BORDER);
		fd_label_8.right = new FormAttachment(text, -19);
		fd_button_2.left = new FormAttachment(text, 26);
		FormData fd_text = new FormData();
		fd_text.left = new FormAttachment(0, 117);
		fd_text.right = new FormAttachment(100, -661);
		fd_text.top = new FormAttachment(button_1, 2, SWT.TOP);
		text.setLayoutData(fd_text);
		
		Button button_3 = new Button(composite, SWT.NONE);
		fd_button_2.right = new FormAttachment(button_3, -74);
		fd_button_1.left = new FormAttachment(button_3, 81);
		FormData fd_button_3 = new FormData();
		fd_button_3.top = new FormAttachment(button_1, 0, SWT.TOP);
		fd_button_3.left = new FormAttachment(0, 410);
		fd_button_3.right = new FormAttachment(100, -368);
		button_3.setLayoutData(fd_button_3);
		button_3.setText("删除");
		
		text_3 = new Text(composite, SWT.BORDER);
		FormData fd_text_3 = new FormData();
		fd_text_3.left = new FormAttachment(button_1, -73);
		fd_text_3.right = new FormAttachment(button_1, 0, SWT.RIGHT);
		fd_text_3.top = new FormAttachment(table, 32);
		text_3.setLayoutData(fd_text_3);

		menu.addMenuListener(new MenuAdapter() {
			@Override
			public void menuShown(MenuEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String status = String.valueOf(ti.getData("c_status"));
				if("0".equals(status)) {
					menuItem.setEnabled(false);
					menuItem_1.setEnabled(true);
				}else {
					menuItem.setEnabled(true);
					menuItem_1.setEnabled(false);
				}
			}
		});

		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String account = ti.getText(1);//获取第二列的值，即用户名字
				AddCarDao addcardao = new AddCarDao();
				int result = addcardao.update(0 , account);
				if(result > 0) {
					finds();
				}
			}
		});

		menuItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String account = ti.getText(1);//获取第二列的值，即用户名字
				AddCarDao addcardao = new AddCarDao();
				int result = addcardao.update(1, account);
				if(result > 0) {
					finds();
				}	
			}
		});

		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AddCarDao addcardao = new AddCarDao();
				String type = combo.getText().trim();
				String cname = text_2.getText().trim();
				String startstation = text_3.getText().trim();
				String destination = text_4.getText().trim();
				String starttime = text_5.getText().trim();
				String endtime = text_6.getText().trim();
				List<Map<String, String>> list = addcardao.finds();
				for(Map<String, String> map : list) {
					if(cname.equals(map.get("c_name"))) {
						MessageDialog.openWarning(getShell(), "温馨提示", "列车名字已存在....");
						return;
					}
				}
				if(StringUtil.checkNull(type,cname,startstation,destination,starttime,endtime)) {
					MessageDialog.openWarning(getShell(), "温馨提示", "列车信息不完整....");
					return;
				}
				if(StringUtil.checkNameChese(startstation) && StringUtil.checkNameChese(destination)) {
				}else {
					MessageDialog.openWarning(getShell(), "温馨提示", "站点请输入汉字....");
					return;
				}
				String str = starttime.substring(0, 2);
				String str1 = starttime.substring(3, 5);
				String str2 = endtime.substring(0, 2);
				String str3 = endtime.substring(3, 5);
				int i = str.compareTo("00");
				int j = str.compareTo("24");
				int m = str1.compareTo("00");
				int n = str1.compareTo("60");
				int a = str2.compareTo("00");
				int b = str2.compareTo("24");
				int c = str3.compareTo("00");
				int d = str3.compareTo("60");
				int t = starttime.compareTo(endtime);
				if(t > 0) {
					MessageDialog.openWarning(getShell(), "温馨提示", "我们是短途旅行，时长不超过一天，....");
					return;
				}
				if( i >= 0 || j < 0 || m >= 0 || n < 0 || a >= 0 || b < 0 || c >= 0 || d < 0) {
				}else {
					MessageDialog.openWarning(getShell(), "温馨提示", "请输入正确的时间....");
					return;
				}
				int result = addcardao.add(type, cname, startstation, destination, starttime, endtime);
				if(result > 0) {
					SWTUtil.showMessage(getShell(), "提示", "添加成功！");
					finds();
				}else {
					SWTUtil.showMessage(getShell(), "提示", "添加失败！");
					return;
				}
			}
		});

		button_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				AddCarDao addcardao = new AddCarDao();
				String id = text.getText().trim();
				String type = combo.getText().trim();
				String cname = text_2.getText().trim();
				String startstation = text_3.getText().trim();
				String destination = text_4.getText().trim();
				String starttime = text_5.getText().trim();
				String endtime = text_6.getText().trim();
				if(StringUtil.checkNull(type,cname,startstation,destination,starttime,endtime)) {
					MessageDialog.openWarning(getShell(), "温馨提示", "列车信息不完整....");
					return;
				}
				if(StringUtil.checkNameChese(startstation) && StringUtil.checkNameChese(destination)) {
				}else {
					MessageDialog.openWarning(getShell(), "温馨提示", "站点请输入汉字....");
					return;
				}
				String str = starttime.substring(0, 2);
				String str1 = starttime.substring(3, 5);
				String str2 = endtime.substring(0, 2);
				String str3 = endtime.substring(3, 5);
				int i = str.compareTo("00");
				int j = str.compareTo("24");
				int m = str1.compareTo("00");
				int n = str1.compareTo("60");
				int a = str2.compareTo("00");
				int b = str2.compareTo("24");
				int c = str3.compareTo("00");
				int d = str3.compareTo("60");
				int t = starttime.compareTo(endtime);
				if(t > 0) {
					MessageDialog.openWarning(getShell(), "温馨提示", "我们是短途旅行，时长不超过一天，....");
					return;
				}
				if( i >= 0 || j < 0 || m >= 0 || n < 0 || a >= 0 || b < 0 || c >= 0 || d < 0) {
				}else {
					MessageDialog.openWarning(getShell(), "温馨提示", "请输入正确的时间....");
					return;
				}
				int result = addcardao.updateTrainInfo(cname, type, startstation, destination, starttime, endtime , id);
				if(result > 0) {
					SWTUtil.showMessage(getShell(), "提示", "修改成功！");
					finds();
				}else {
				SWTUtil.showMessage(getShell(), "提示", "修改失败！");
				return;
				}
			}
		});
		button_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				AddCarDao addcardao = new AddCarDao();
				String cname = text_2.getText().trim();
				int result = addcardao.delete(cname);
				if(result > 0) {
					SWTUtil.showMessage(getShell(), "提示", "删除成功！");
					finds();
				}else {
					SWTUtil.showMessage(getShell(), "提示", "删除失败！");
					return;
				}
				
			}
		});

		finds();
	}
	protected void finds() {
		AddCarDao addcardao = new AddCarDao();
		List<Map<String, String>> list = addcardao.finds();//获取所有列车的信息
		//将所有列车信息显示在表格中
		table.removeAll();//移除表格中原有的数据
		if(list == null || list.isEmpty()) {
			return;
		}
		TableItem ti = null;//行对象
		for(Map<String, String> map : list) {
			ti = new TableItem(table, SWT.NONE);
			ti.setData("c_status", map.get("c_status"));
			if("0".equals(map.get("c_status"))) {
				ti.setText(new String[] {map.get("c_id"), map.get("c_name"),map.get("c_startstation"), map.get("c_destination"),map.get("c_starttime"), map.get("c_endtime"), "正常"});
			}else {
				ti.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
				ti.setText(new String[] {map.get("c_id"), map.get("c_name"), map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"), map.get("c_endtime"), "停运"});
			}
		}
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
